import os
import requests
from bs4 import BeautifulSoup

# 定义要爬取的 URL
url = 'http://books.toscrape.com/'

# 发送 HTTP 请求获取网页内容
response = requests.get(url)
response.raise_for_status()  # 检查请求是否成功

# 使用 BeautifulSoup 解析 HTML 内容
soup = BeautifulSoup(response.text, 'lxml')

# 查找所有图书的容器
books = soup.find_all('article', class_='product_pod')

# 创建一个目录来保存图书图片
os.makedirs('book_images', exist_ok=True)

# 遍历每本图书，提取所需信息
for book in books:
    # 提取图书标题
    title = book.h3.a['title']
    
    # 提取图书价格
    price = book.find('p', class_='price_color').text
    
    # 提取图书评分
    rating = book.p['class'][1]
    
    # 提取图书图片 URL
    image_url = book.find('img')['src']
    image_url = url + image_url.replace('../', '')
    
    # 下载图书图片
    image_response = requests.get(image_url)
    image_response.raise_for_status()
    
    # 保存图书图片
    image_path = os.path.join('book_images', f"{title}.jpg")
    with open(image_path, 'wb') as f:
        f.write(image_response.content)
    
    # 打印图书信息
    print(f'Title: {title}')
    print(f'Price: {price}')
    print(f'Rating: {rating}')
    print(f'Image URL: {image_url}')
    print('-' * 40)

# 保存数据到文件（可选）
with open('books.csv', 'w', encoding='utf-8') as f:
    f.write('Title,Price,Rating,Image URL\n')
    for book in books:
        title = book.h3.a['title']
        price = book.find('p', class_='price_color').text
        rating = book.p['class'][1]
        image_url = book.find('img')['src']
        image_url = url + image_url.replace('../', '')
        f.write(f'"{title}","{price}","{rating}","{image_url}"\n')